package com.rk.cmn.controller;
import com.alibaba.excel.EasyExcel;
import com.rk.cmn.listener.DickListener;
import com.rk.cmn.service.DictService;
import com.rk.common.result.Result;
import com.rk.hosp.model.cmn.Dict;
import com.rk.hosp.vo.cmn.DictEeVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;

/**
 * @author :Rk.
 * @date : 2022/11/3
 */
@Api(tags = "数据字典接口")
@RestController
//@CrossOrigin
@RequestMapping("/admin/cmn/dict")
public class DictController {

    @Autowired
    private DictService dictService;

    @ApiOperation("查询当前节点下的子节列表")
    @GetMapping("/findChildData/{id}")
    public Result findChlidData(@PathVariable Long id){
        List<Dict> chlidData = dictService.findChlidData(id);
        return Result.ok(chlidData);
    }

    @ApiOperation(value="导出")
    @GetMapping(value = "/exportData")
    public void exportData(HttpServletResponse response) {
        dictService.exportData(response);
    }

    @ApiOperation(value="模板下载")
    @GetMapping(value = "/downModel")
    public Result exportDdownModelata(HttpServletResponse response) {
        try {
            dictService.downModel(response);
            return Result.ok();
        } catch (IOException e) {
            e.printStackTrace();
            return Result.fail();
        }
    }


    @ApiOperation(value = "导入")
    @CacheEvict(value = "dict",allEntries = true)
    @PostMapping("/importData")
    public Result importData(MultipartFile file) throws IOException {
        //0代表读取excel的sheet1
        try {
            EasyExcel.read(file.getInputStream(), DictEeVo.class,new DickListener(dictService)).sheet(0).doRead();
            return Result.ok();
        } catch (IOException e) {
            e.printStackTrace();
            return Result.fail("文件解析失败，请检查文件内容!");
        }
    }


    @ApiOperation(value = "获取字典名称")
    @GetMapping("/getName/{parentDictCode}/{value}")
    @Cacheable(value = "dict",keyGenerator = "keyGenerator")
    public String  getName(
            @ApiParam(name = "parentDictCode", value = "上级编码", required = true)
            @PathVariable("parentDictCode") String parentDictCode,
                          @ApiParam(name = "value", value = "值", required = true)
                          @PathVariable("value") String value){
        String name="";
        try {
             name = dictService.getNameByParentDictCodeAndValue(parentDictCode,value);
        }catch (Exception e){
            e.printStackTrace();
            return Result.fail("查询失败").getMessage();
        }
        return name;
    }


    @ApiOperation(value = "获取字典名称")
    @GetMapping("/getName//{value}")
    @Cacheable(value = "dict",keyGenerator = "keyGenerator")
    public String  getName(@ApiParam(name = "value", value = "值", required = true)
                          @PathVariable("value") String value){
        String name="";
        try {
            name=dictService.getNameByParentDictCodeAndValue("",value);
        }catch (Exception e){
            return Result.fail("查询失败").getMessage();
        }
        return name;
    }


    @ApiOperation(value = "通过dictCode查询子节点")
    @GetMapping("/findByDictCode/{dictCode}")
    public Result<List<Dict>> findByDictCode(@PathVariable("dictCode")String dictCode){
        List<Dict> list = dictService.findByDictCode(dictCode);
        return Result.ok(list);
    }

}
